package com.jed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jed.R;
import com.jed.entity.JiCai;
import com.jed.util.L;
import com.jed.util.StaticClass;
import com.jed.util.StringUtil;
import com.jed.util.T;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.Date;

/**
 * 项目名：XMKAPP<br/>
 * 包名：  com.jed.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/28 14:24<br/>
 * 描述：集采审核页<br/>
 */
public class JiCaiActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_xmmingcheng;
    private TextView tv_zhubanchushi;
    private TextView tv_isjicai;
    private TextView tv_yusuanjine;

    private Button btn_back;
    private Button btn_pass;

    private static final int PASS = 1;
    private static final int BACK = 3;
    public static final int RESULT_CODE = 1;

    private int position;
    private JiCai jiCai;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jicai);
        dialog.show();
        initView();
        dialog.dismiss();

    }

    private void initView() {

        tv_xmmingcheng = findViewById(R.id.tv_xmmingcheng);
        tv_zhubanchushi = findViewById(R.id.tv_zhubanchushi);
        tv_isjicai = findViewById(R.id.tv_isjicai);
        tv_yusuanjine = findViewById(R.id.tv_yusuanjine);
        btn_back = findViewById(R.id.btn_back);
        btn_pass = findViewById(R.id.btn_pass);
        btn_back.setOnClickListener(this);
        btn_pass.setOnClickListener(this);

        Intent intent = getIntent();
        String json = intent.getStringExtra("jiCai");
        jiCai = new Gson().fromJson(json, JiCai.class);
        position = intent.getIntExtra("position", -1);

        tv_xmmingcheng.setText(jiCai.getXmmingcheng());
        tv_zhubanchushi.setText(jiCai.getZhubanchushi());
        tv_isjicai.setText(jiCai.getIsjicai());
        tv_yusuanjine.setText(StringUtil.doubleToStr(jiCai.getYusuanjine()));
    }

    @Override
    public void onClick(View v) {
        dialog.show();
        switch (v.getId()) {
            case R.id.btn_back:
                updateJiCai(jiCai.getId(), BACK, "移动端审核退回" + new Date().toString());
                break;
            case R.id.btn_pass:
                updateJiCai(jiCai.getId(), PASS, "");
                break;
        }

    }

    public void updateJiCai(String id, final int state, String reason) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("id", id);
        httpParams.put("state", state);
        httpParams.put("reason", reason);
        RxVolley.post(StaticClass.JI_CAI_UPDATE_URL, httpParams, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                dialog.dismiss();
                L.i("更新结果为：" + t);
                if ("1".equals(t)&& state== PASS) {
                    jiCai.setShenhezhuangtai(JiCai.PASS);
                }else if ("1".equals(t)&& state== BACK) {
                    jiCai.setShenhezhuangtai(JiCai.BACK);
                }
                Intent intent = new Intent();
                intent.putExtra("jiCai", StringUtil.toGson(jiCai));
                intent.putExtra("position", position);
                setResult(RESULT_CODE, intent);
                finish();
            }
            @Override
            public void onFailure(int errorNo, String strMsg) {
                dialog.dismiss();
                L.i("errorNo：" + errorNo);
                L.i("strMsg：" + strMsg);
                T.showShortToast(getResources().getString(R.string.connection_timeout));

            }
        });


    }

}
